package com.syt.service.controller;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.alibaba.fastjson.JSONObject;
import com.syt.service.pojo.PhoneRecommend;
import com.syt.service.service.PhoneRecommendService;
import com.syt.service.util.LoginRequired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * @author ：syt
 * @date ：Created in 2019-10-02 14:13
 * @description：首页推荐轮播图控制器
 */
@RestController
public class PhoneRecommendController {

    @Autowired
    private PhoneRecommendService service;
    @Value("${syt.upload}")
    private String uploadpath;

    /**
     * 获取推荐列表
     * @return
     */
    @RequestMapping(value = "recommend/list", method = RequestMethod.GET)
    public List<PhoneRecommend> getRecommend() {
        List<PhoneRecommend> recommendList = service.list();
        return recommendList;
    }

    /**
     * 添加推荐列表, 先保存图片
     * @param recommend
     * @return
     */
    @PostMapping("recommend/set")
    @LoginRequired
    public Object setRecommend(PhoneRecommend recommend, @RequestAttribute String userId, MultipartFile file, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        // 上传文件
        RestTemplate client = new RestTemplate();
        URI uri = client.postForLocation(uploadpath, request, String.class);
        jsonObject.put("url", uri);
        jsonObject.put("recommend", recommend);
//        jsonObject.put("message", service.insertBySelective(recommend));
        return jsonObject;
    }

    /***
     * 需要 服务器搭建 推荐图上传，待完成
     * @param upload
     * @return
     * @throws IOException
     */
    @PostMapping("recommend/fileUp")
    @LoginRequired
    public Object fileUp(MultipartFile upload) {
        JSONObject jsonObject = new JSONObject();

        return jsonObject;
    }
}
